package com.market.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.Dto.EmployeeLoginDto;
import com.market.Dto.EmployeeQueryDto;
import com.market.Dto.EmployeeSaveDto;
import com.market.Vo.EmployeeQueryVo;
import com.market.entity.Employee;
import com.market.entity.Records;
import com.market.mapper.EmployeeMapper;
import com.market.service.EmployeeService;
import com.market.utils.BaseContext;
import com.market.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private
    EmployeeMapper employeeMapper;
    /**
     * 登录
     * @param employeeLoginDto
     * @return
     */
    @Override
    public Employee login(EmployeeLoginDto employeeLoginDto) {
        String username=employeeLoginDto.getUsername();
        String password=employeeLoginDto.getPassword();

        Employee employee=employeeMapper.getByName(username);
        if(employee==null){
            throw new RuntimeException("用户名不存在");
        }
        if(!employee.getPassword().equals(password))
            throw new RuntimeException("密码错误");

        return employee;

    }

    /**
     * 员工分页查询
     * @param employeeQueryDto
     * @return
     */
    @Override
    public EmployeeQueryVo list(EmployeeQueryDto employeeQueryDto) {

        PageHelper.startPage(employeeQueryDto.getPage(),employeeQueryDto.getLimit());
        List<Employee> employeeList=employeeMapper.pageQuery(employeeQueryDto);
        PageInfo<Employee> pageInfo=new PageInfo<>(employeeList);
        EmployeeQueryVo employeeQueryVo=new EmployeeQueryVo();
        employeeQueryVo.setTotal((int)pageInfo.getTotal());
        employeeQueryVo.setRecords(pageInfo.getList());
        return employeeQueryVo;
    }

    /**
     * 获取员工详情
     * @param id
     * @return
     */

    @Override
    public Employee getById(Long id) {
        Employee employee=employeeMapper.getById(id);
        return employee;
    }

    /**
     * 新增员工
     * @param employeeSaveDto
     */
    @Override
    public void  saveEmployee(EmployeeSaveDto employeeSaveDto) {
        Employee employee=new Employee();
        BeanUtils.copyProperties(employeeSaveDto,employee);
        employeeMapper.save(employee);
    }

    /**
     * 删除员工
     * @param id
     */
    @Override
    public void deleteEmployee(Long id) {
        employeeMapper.deleteById(id);
    }

    /**
     * 修改员工
     * @param employeeSaveDto
     */
    @Override
    public void updateEmployee(EmployeeSaveDto employeeSaveDto) {
        Long userId=BaseContext.getCurrentId();
        log.info("当前用户id为：{}",userId);
        Employee employee=new Employee();
        BeanUtils.copyProperties(employeeSaveDto,employee);
        employeeMapper.update(employee);
    }
}
